package com.other;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baseutils.SpfUtils;
import com.github.czy1121.view.RoundButton;
import com.julis.R;
import com.julis.db.Z_ObuInfoDao;
import com.julis.ui.horizon.HorizontalProgressViewModel;
import com.julis.ui.horizon.Node;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.thread.CheckOBUThread;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 确认设备信息
 */
public class N_QRSBXX4_Activity extends AppCompatActivity {


    @BindView(R.id.img_back)
    RelativeLayout imgBack;


    @BindView(R.id.vin_code)
    TextView vin;
    @BindView(R.id.obu_code)
    TextView obu;
    @BindView(R.id.textView14)
    TextView textView14;
    @BindView(R.id.bt_bzq)
    RoundButton btYz;
    @BindView(R.id.bt_next)
    RoundButton btByz;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    Z_ObuInfoDao z_obuInfoDao;
    String uuid;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.textView16)
    TextView textView16;
    @BindView(R.id.obutradevalue)
    TextView obucheckvalue;
    @BindView(R.id.lanya)
    TextView lanya;
    @BindView(R.id.blecheckvalue)
    TextView blecheckvalue;
    @BindView(R.id.vininputvalue)
    TextView vininputvalue;
    @BindView(R.id.check_layout)
    LinearLayout base;
    String OBU;
    @BindView(R.id.layout_vininput)
    RelativeLayout Relatvins;
    @BindView(R.id.layout_vinwrite)
    RelativeLayout Relatvins2;
    @BindView(R.id.layout_bluetooth)
    RelativeLayout layout_lanya;
    boolean lanyas;
    int zhi = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_qrsbxx2);
        ButterKnife.bind(this);
        if ((boolean) SpfUtils.get(this, "vinshuru", true)) {
            Relatvins.setVisibility(View.VISIBLE);
            Relatvins2.setVisibility(View.VISIBLE);
            zhi = 3;
        } else {
            Relatvins.setVisibility(View.GONE);
            Relatvins2.setVisibility(View.GONE);
            zhi = 2;
        }
        lanyas = (boolean) SpfUtils.get(this, "lanya", true);
        if (lanyas) {
            layout_lanya.setVisibility(View.VISIBLE);
        } else {
            layout_lanya.setVisibility(View.GONE);
        }
        base.setVisibility(View.GONE);
        uuid = getIntent().getStringExtra("uuid");
        z_obuInfoDao = new Z_ObuInfoDao(this);
        String vin = (String) SpfUtils.get(N_QRSBXX4_Activity.this, "VIN", "");
        this.vin.setText("VIN码：" + vin);
        OBU = (String) SpfUtils.get(N_QRSBXX4_Activity.this, "OBU", "");
        obu.setText("OBU MAC号：" + OBU);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        // 引用
        HorizontalProgressViewModel model = new HorizontalProgressViewModel();
        model.setViewUp(this, recyclerView, getProgressList());
        checks();

    }

    KProgressHUD dialog1, dialog2, dialog3;

    public void showdialog1() {
        dialog1 = KProgressHUD.create(N_QRSBXX4_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在检测OBU")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    public void showdialog2() {
        dialog2 = KProgressHUD.create(N_QRSBXX4_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在检测蓝牙")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    public void showdialog3() {
        dialog3 = KProgressHUD.create(N_QRSBXX4_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在写入VIN")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }


    /**
     * 模拟节点数据
     * node1.nodeStatus: 0 已完成状态  1正在处理状态  -1待处理状态
     *
     * @return
     */
    private List<Node> getProgressList() {
        List<Node> list = new ArrayList<>();
        if ((boolean) SpfUtils.get(this, "vinshuru", true)) {
            Node node1 = new Node();
            Node node2 = new Node();
            Node node4 = new Node();
            node1.nodeName = "录入VIN";
            node1.nodeStatus = 0;
            node2.nodeName = "录入OBU";
            node2.nodeStatus = 0;
            node4.nodeName = "设备检测";
            node4.nodeStatus = 0;
            list.add(node1);
            list.add(node2);
            list.add(node4);
        } else {
            Node node2 = new Node();
            Node node4 = new Node();
            node2.nodeName = "录入OBU";
            node2.nodeStatus = 0;
            node4.nodeName = "设备检测";
            node4.nodeStatus = 0;
            list.add(node2);
            list.add(node4);
        }


        return list;

    }

    String gettime;

    @OnClick({R.id.img_back, R.id.bt_bzq, R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                setting();
                break;
            case R.id.bt_bzq:
                checks();
                break;
            case R.id.bt_next:
                setting();

                break;
        }
    }

    public void setting() {



        Toast.makeText(this, "记录保存成功！", Toast.LENGTH_SHORT).show();

        finish();
    }


    @SuppressLint("HandlerLeak")
    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if (dialog1 != null) {
                Log.e("v", "走了Q");
                dialog1.dismiss();
                if (msg.what == 1) {
                    obucheckvalue.setTextColor(getResources().getColor(R.color.ym_green));
                    obucheckvalue.setText("成功");

                } else {
//                    Toast.makeText(N_QRSBXX3_Activity.this, ""+msg.obj, Toast.LENGTH_SHORT).show();
                    obucheckvalue.setTextColor(getResources().getColor(R.color.red));
                    obucheckvalue.setText("" + msg.obj);

                }
                if (lanyas) {
                    showdialog2();

                } else {


                }
//                mHandler.postDelayed(r, value);//延时100毫秒

            }

            super.handleMessage(msg);
        }
    };
    int a = 1;
    int value = 2000;
    Handler mHandler = new Handler();
//    Runnable r = new Runnable() {
//
//        @Override
//        public void run() {
//            //do something
//            //每隔1s循环执行run方法
//
//
//            if (a == 1) {
//                value = 2000;
//                Log.e("zzz", "走了A");
//                if (dialog2 != null) {
//                    Log.e("zzz", "蓝牙消失");
//                    dialog2.dismiss();
//                    if (w.equals("成功")) {
//                        blecheckvalue.setText("成功");
//                        blecheckvalue.setTextColor(getResources().getColor(R.color.ym_green));
//                    } else {
//                        blecheckvalue.setText("失败");
//                        blecheckvalue.setTextColor(getResources().getColor(R.color.red));
//                    }
//                    if (zhi == 3) {
//                        showdialog3();
//                    } else {
//                        value = 500;
//                    }
//
//
//                }
//            }
//            if (a == 2) {
//                value = 500;
//                Log.e("zzz", "走了B");
//                if (dialog3 != null) {
//                    Log.e("zzz", "VIN消失");
//                    dialog3.dismiss();
//                    if (w.equals("成功")) {
//                        vininputvalue.setText("成功");
//                        vininputvalue.setTextColor(getResources().getColor(R.color.ym_green));
//                    } else {
//                        vininputvalue.setText("失败");
//                        vininputvalue.setTextColor(getResources().getColor(R.color.red));
//                    }
//
//
//                }
//            }
//            if (a <= zhi) {
//                mHandler.postDelayed(r, value);//延时100毫秒
//                a++;
//                Log.e("zzz", "数据叠加" + a);
//            } else {
//                base.setVisibility(View.VISIBLE);
//                Log.e("zzz", "2数据叠加" + a);
//                if (dialog1 != null) {
//                    dialog1.dismiss();
//                }
//                if (dialog2 != null) {
//                    dialog2.dismiss();
//                }
//                if (dialog3 != null) {
//                    dialog3.dismiss();
//                }
//            }
//
//
//        }
//
//
//    };

    public void checks() {
        value = 2000;
        base.setVisibility(View.GONE);
        obucheckvalue.setText(" ");
        vininputvalue.setText(" ");
        blecheckvalue.setText(" ");
        vininputvalue.setTextColor(getResources().getColor(R.color.ym_green));
        obucheckvalue.setTextColor(getResources().getColor(R.color.ym_green));
        blecheckvalue.setTextColor(getResources().getColor(R.color.ym_green));
        a = 1;
        showdialog1();
        CheckOBUThread activateOBUThread = new CheckOBUThread(N_QRSBXX4_Activity.this, OBU, handler1);
        activateOBUThread.start();
    }

}
